package by.yakusekmaxim.bookgid.services.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import by.yakusekmaxim.bookgid.dataaccess.UserAccount2BookDao;
import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book;
import by.yakusekmaxim.bookgid.services.UserAccount2BookService;


@Repository
public class UserAccount2BookServiceImpl implements UserAccount2BookService {



	private static final Logger LOGGER = LoggerFactory
			.getLogger(UserAccount2BookServiceImpl.class);
	
	@Inject
	private UserAccount2BookDao dao;
	
	@Override
	public UserAccount2Book get(Long id) {
		UserAccount2Book entity = dao.getById(id);
		return entity;
	}
	
	@Override
	public void delete(UserAccount2Book userAccount2Book) {
		LOGGER.debug("Remove: {}", userAccount2Book);
		dao.delete(userAccount2Book.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all userAccount2Book");
		dao.deleteAll();
	}
	
	
	@Override
	public void saveOrUpdate(UserAccount2Book userAccount2Book) {
		if (userAccount2Book.getId() == null) {
			LOGGER.debug("Save new: {}", userAccount2Book);
			dao.insert(userAccount2Book);
		} else {
			LOGGER.debug("Update: {}", userAccount2Book);
			dao.update(userAccount2Book);
		}
	}

	@Override
	public List<UserAccount2Book> getAllUserAccount2BookDao(
			SingularAttribute<UserAccount2Book, ?> attribute,
			boolean ascending, int startRecord, int pageSize, UserAccount user) {
		return dao.getAllUserAccount2BookDao(attribute, ascending, startRecord, pageSize, user);
	}

	@Override
	public List<UserAccount2Book> getAllUserAccount2BookDao(
			SingularAttribute<UserAccount2Book, ?> attribute,
			boolean ascending, int startRecord, int pageSize,
			Filter<UserAccount2Book> filter, UserAccount user) {
		return dao.getAllUserAccount2BookDao(attribute, ascending, startRecord, pageSize, filter, user);
	}

	@Override
	public UserAccount2Book getUserAccount2BookDao(Book book, UserAccount user) {
		List<UserAccount2Book> list = dao.getUserAccount2BookDao(book, user);
		UserAccount2Book userAccount2Book = null;
		if (list != null & list.size()!=0) {
			userAccount2Book = list.get(0);
		}
		return userAccount2Book;
	}

	@Override
	public Long getCountByUserAccount(UserAccount user) {
		return dao.getCountByUserAccount(user);
	}

	@Override
	public Long getCountByUserAccount(Filter<UserAccount2Book> filter,
			UserAccount user) {
		return dao.getCountByUserAccount(filter, user);
	}

	@Override
	public Long getCountByBook(Filter<UserAccount2Book> filter, Book book) {
		return dao.getCountByBook(filter, book);
	}

}
